package com.shenmazong;

import redis.clients.jedis.Jedis;

import java.util.*;

/**
 * @program: server-java-demo
 * @description: RedisLockTest
 * @author: 亮子说编程
 * @create: 2020-11-10 14:18
 **/
public class RedisLockTest {

    public static void main(String[] args) throws InterruptedException {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        System.out.println("连接成功");


        do {
            String lockName = "lock_name";
            String lockValue = UUID.randomUUID().toString();
            Long setnx = jedis.setnx(lockName, lockValue);

            if(setnx == 1) {
                jedis.expire(lockName, 10);

                System.out.println(Calendar.getInstance().getTime().toString() + " ： get OK");
            }
            else {
                System.out.println(Calendar.getInstance().getTime().toString() + " ： get Failed");
                Thread.sleep(1000);
            }
        } while(true);


    }
}
